myApp.controller('contentCtrl', ['$scope', '$location', '$http',function ($scope, $location,$http) {

    $scope.contents = {};
    _log('### contentCtrl ###');

    var url = 'content_ws.php';
            
    var renderTable = function(){
	var t = $('#table').dataTable( {
            "ajax":{ 
                    url: _COMMON.context + url+"?wsname=getallcontent&token=1234",
                    dataSrc: "contents"
                },
            "columns": [
                { "data": "id",  "sClass" : "center"},
                { "data": "title" },
                { "data": "catid" },
                { "data": "alias" },
                { "data": "userid" },
                { "data": "modified" , "sClass" : "center"},
                { "data": "publishdate" , "sClass" : "center"},
                { "data": "status",  "sClass" : "center"}
                
            ],
            "columnDefs": [ 
                   {
                       "render": function ( data, type, row ) {
                           return $scope.categoryMap[data];
                       },
                       "targets": 2
                   },
                   {
                       "render": function ( data, type, row ) {
                	   return $scope.usersMap[data];
                       },
                       "targets": 4
                   },
                   {
                       "render": function ( data, type, row ) {
                	   return '<font style="color: '+_COMMON.enum.status[data].color+'">'
                	   +_COMMON.enum.status[data].value+'</font>';
                       },
                       "targets": 7
                   },
                   {
                       "render": function ( data, type, row ) {
                	   return '<button title="Edit" type="button" class="btn btn-warning btn-table-action" onclick="_pageFn.onContentEdit('+data+')"><i class="fa fa-nontext fa-edit"></i></button>&nbsp;'+
                   	'<button title="Delete"type="button" class="btn btn-danger btn-table-action" onclick="_pageFn.onContentDelete('+data+')"><i class="fa fa-nontext fa-trash-o"></i></button>';
                       },
                       "targets": 0
                   }
               ],
//		"sScrollY": "300px",
		"sScrollX": "100px",
		"sScrollXInner": "100%",
		"bScrollCollapse": true,
		"bPaginate": true,
		"bJQueryUI": false,
		"sPaginationType": "full_numbers"
                      
        } );
        
//	new $.fn.dataTable.FixedColumns( t, {
//		"iLeftColumns": 1,
//		"iRightColumns": 0
//	} );
        
    };
    
    
    
    ///------------------------------------ Static Data -----
    $scope.categoryMap = {};
    $scope.usersMap = {};
    
    var url_2 = 'category_ws.php';
    var params_2 = {
        wsname: 'getallcategories',
        token: _pageData.tokenKey
    };
    $.getJSON(_COMMON.context + url_2, params_2).done(function (result) {
        $scope.$apply(function () {
            for (i in result.categories) {
                var val  = '{"'+ result.categories[i]['id'] +'" : "' + result.categories[i]['name']+'"}';
                var obj = JSON.parse(val);
                $scope.categoryMap =  $.extend(true, {}, $scope.categoryMap , obj); // Merge json
            }
            
            // ------- User Data Array
            var url_3 = 'user_ws.php';
            var params_3 = {
                wsname: 'getallusers',
                token: _pageData.tokenKey
            };
            $.getJSON(_COMMON.context + url_3, params_3).done(function (result) {
                 $scope.$apply(function () {
                    for (i in result.users) {
                        var val  = '{"'+ result.users[i]['id'] +'" : "' + result.users[i]['username']+'"}';
                        var obj = JSON.parse(val);
                        $scope.usersMap =  $.extend(true, {}, $scope.usersMap , obj); // Merge json
                    }
                    
                    // Start Render table 
                    renderTable();
                    
                });
            });
            
        });
    });

    
   
    ///------------------------------------ Static Data -----
    
    _pageFn.onContentEdit = function (id) {
	location = '#/admin/content/content?contentid='+id;
    };

    _pageFn.onContentDelete = function (id) {

        $("#confirm").html("Do you want to delete ?");

        $("#confirm").dialog({
        resizable: false,
        modal: true,
        title: "Confirm Delete",
        height: 180,
        width: 300,
        buttons: {
            "Yes": function () {
                $(this).dialog('close');
                callback(id);
            },
                "No": function () {
                $(this).dialog('close');
            }
        }
    });
    };
       
    
    function callback(value) {
        if (value) {
      
             var config = {
	            params : {
		        wsname : 'deletecontent',
		        token : _pageData.tokenKey,
		        id : value
                }
	        };
	
	        $http.post(_COMMON.context + url, null, config)
	          .success(function(obj) {
                    notif({
                      msg: "<b><i class='fa fa-thumbs-o-up'></i>  Delete Success !</b>",
                      type: "success",
                      position: "center"
                    });

                    location= '#/admin/content/list/'; //refresh.

                  })
                  .error(function() {
                      alert('Error is occured, May be database connect is wrong');
                  });
       

        } else {
            alert("Rejected");
        }

    }


} ]);
